<?php 
namespace Home\Controller;
class ZxwdController extends PublicController{
	function index(){
		$na = M("Question");
		$arr2 = $na->limit(20)->select();
		$this->assign('arr2',$arr2);
		$arr = $na->limit(10)->select();
		$arr3 = $na->limit(4)->select();
		$this->assign('arr3',$arr3);
		$Ob = M("Answer");
		$arr1 = $Ob->limit(10)->select();
		$this->assign('arr1',$arr1);
		$this->assign('arr',$arr);

		$this->getip();
		$this->display();
	}
	function ask(){
		$Ob = M("Zxask");
		$arr = $Ob->select();
		$this->assign('arr',$arr); 
		
		$this->display();
	}
	function change(){
		$Ob1 = M("Fenlei");
		$fid=$_POST["fid"];
		$arr1 =$Ob1->where("fid=$fid")->select();
		$opt = '<option>--请选择--</option>';
			foreach($arr1 as $key=>$val){
			    $opt .= "<option  value='{$val['name']}'>{$val['name']}</option>";
			 }
			 if($arr1){
			 	echo json_encode($opt);
			 }
	}
	function tianjia(){
		$que = M("Question");
		session_start();
		$_POST["uid"]=$_SESSION["id"];
		$_POST["username"]=$_SESSION["username"];
		$_POST["addtime"]=time();
		$re = $que->data($_POST)->add();
		if($re){
			 echo "<script>alert('提问成功');parent.location.href='index';</script>";
		}
	}
	//{:U('Home/Zxwd/nav',array('fid'=>0,'name'=>$v['id']))}
	function nav(){
		$Ob = M("Fenlei");
		$arr1 = $Ob->where("fid=0")->select();
		$this->assign('arr1',$arr1);
		$na = M("Question");
		$arr2 = $na->limit(20)->select();
		$this->assign('arr2',$arr2);
		$num=$na->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$name=$_GET["name"];
		if(!$name){
			$arr = $na->where("fid=0")->limit("$start,$pageSize")->order('id desc')->select();
		}else{
			$arr = $na->where("fid=0 AND name='$name'")->limit("$start,$pageSize")->order('id desc')->select();
		}
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
		}

		$this->getip();
        $this->display();
	}
	function liucheng(){
		$Ob = M("Fenlei");
		$arr1 = $Ob->where("fid=1")->select();
		$this->assign('arr1',$arr1);
		$na = M("Question");
		$arr2 = $na->limit(20)->select();
		$this->assign('arr2',$arr2);
		$num=$na->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$name=$_GET["name"];
		if(!$name){
			$arr = $na->where("fid=1")->limit("$start,$pageSize")->order('id desc')->select();
		}else{
					
			$arr = $na->where("fid=1 AND name='$name'")->limit("$start,$pageSize")->order('id desc')->select();
		}
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
		}

		$this->getip();
        $this->assign('data',$data);
        $this->display();
	}
	function jiaju(){
		$Ob = M("Fenlei");
		$arr1 = $Ob->where("fid=2")->select();
		$this->assign('arr1',$arr1);
		$na = M("Question");
		$arr2 = $na->limit(20)->select();
		$this->assign('arr2',$arr2);
		$num=$na->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$name=$_GET["name"];
		if(!$name){
			$arr = $na->where("fid=2")->limit("$start,$pageSize")->order('id desc')->select();
		}else{
					
			$arr = $na->where("fid=2 AND name='$name'")->limit("$start,$pageSize")->order('id desc')->select();
		}
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
		}

		$this->getip();
        $this->display();
	}
	function cailiao(){
		$Ob = M("Fenlei");
		$arr1 = $Ob->where("fid=3")->select();
		$this->assign('arr1',$arr1);
		$na = M("Question");
		$arr2 = $na->limit(20)->select();
		$this->assign('arr2',$arr2);
		$num=$na->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$name=$_GET["name"];
		if(!$name){
			$arr = $na->where("fid=3")->limit("$start,$pageSize")->order('id desc')->select();
		}else{
					
			$arr = $na->where("fid=3 AND name='$name'")->limit("$start,$pageSize")->order('id desc')->select();
		}
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
		}
		$this->getip();
        $this->display();
	}
	function sheji(){
		$Ob = M("Fenlei");
		$arr1 = $Ob->where("fid=4")->select();
		$this->assign('arr1',$arr1);
		$na = M("Question");
		$arr2 = $na->limit(20)->select();
		$this->assign('arr2',$arr2);
		$num=$na->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$name=$_GET["name"];
		if(!$name){
			$arr = $na->where("fid=4")->limit("$start,$pageSize")->order('id desc')->select();
		}else{
					
			$arr = $na->where("fid=4 AND name='$name'")->limit("$start,$pageSize")->order('id desc')->select();
		}
		if($arr){
			$this->assign('pageStr',$pageStr);
			$this->assign('arr',$arr);
		}else{
		}
		$clientIP=get_client_ip();
        $taobaoIP = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$clientIP;
        $IPinfo = json_decode(file_get_contents($taobaoIP));
        $province = $IPinfo->data->region;
        $city = $IPinfo->data->city;
        $data = $city; 
        $this->assign('data',$data);
        $this->display();
	}
	function ask_detail(){
		$Ob=M("Question");
		$title=$_GET["title"];
		$arr = $Ob->where("title='$title'")->find();
		$this->assign('arr',$arr);
		$Ob1 = M("Answer");
		$arr1 = $Ob1->where("title='$title'")->select();
		$this->assign('arr1',$arr1);
		$clientIP=get_client_ip();
        $taobaoIP = 'http://ip.taobao.com/service/getIpInfo.php?ip='.$clientIP;
        $IPinfo = json_decode(file_get_contents($taobaoIP));
        $province = $IPinfo->data->region;
        $city = $IPinfo->data->city;
        $data = $city;
        $this->assign('data',$data);
		$this->display();
	}
	function askadd(){
		$Ob = M("Answer");
		$_POST["addtime"] = time();
		session_start();
		$_POST["uid"]=$_SESSION["id"];
		$_POST["username"]=$_SESSION["username"];
		$arr1 = $Ob->data($_POST)->add();
		if($arr1) {
			 echo "<script>alert('回答成功');parent.location.href='index';</script>";
		}else{

		}
	}
	function layout(){
       session_start();
      if(isset($_SESSION['username'])){
           // session_unset();//free all session variable
            session_destroy();//销毁一个会话中的全部数据
            echo "<script>alert('退出成功');parent.location.href='index';</script>";
        }else{
          echo "<script>alert('退出失败');parent.location.href='index';</script>";
        }
    }
}